#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "lm4f120h5qr.h"

//*****************************************************************************
// Intialize ADC
//See page 384 of text book for details
//*****************************************************************************
void initializeADC(void)
{
	uint32_t delay;

	// Enable Port E
	SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R4;
	delay = 10;

	// Enable ADC 
	SYSCTL_RCGCADC_R |= SYSCTL_RCGCADC_R0;
	delay = 20;

	GPIO_PORTE_DIR_R &= ~0x0C;	// Make GPIO_E_2/3 input
	GPIO_PORTE_AFSEL_R |= 0x0C;	// Enable alternate function
	GPIO_PORTE_DEN_R &= ~0x0C;	// Disable digital function
	GPIO_PORTE_AMSEL_R |= 0x0C;	// Enable analog function
	SYSCTL_RCGC0_R |= 0x00010000;	// Activate ADC0
	SYSCTL_RCGC0_R |= 0x00010000;	// Configure to 125K
	ADC0_SSPRI_R = 0x0123;	// Sequence 3 is highest priority
	ADC0_ACTSS_R &= ~0x0008;	// Disable sample sequencer 3
	ADC0_EMUX_R &= ~0xF000;	// Seq3 is software trigger
	ADC0_SSMUX3_R &= ~0x000F;	// Clear SS3 field

	// Set channel
	// ADC0_SSMUX3_R += 1;
	ADC0_SSCTL3_R = 0x0006;	// no TS0 D0, yes IE0 END0
	ADC0_IM_R &= ~0x0008;	// Disable interrupts
	ADC0_ACTSS_R |= 0x0008;	// Enable sample sequencer 3
}
